// 日期+星期+时间
export function formattedDate(timeStr) {
  console.log(typeof (timeStr),timeStr)
  // if ( timeStr=== null || isNaN(timeStr)) {
  //   console.error('传入的startTime格式不正确，期望为合法数字类型的时间戳');
  //   return timeStr; // 可以返回原始值或者一个默认值，根据具体需求调整
  // }
  if (timeStr !== null && timeStr !== undefined && timeStr !== '') {
    // let datestr = moment(timeStr).utcOffset(0).format('YYYY-MM-DD HH:mm:ss');
    // console.log(datestr,'datestr')
    const timeStrss = timeStr.replace('+0000', '');
    const date = new Date(timeStrss);
    const year = date.getFullYear();
    const month = ("0" + (date.getMonth() + 1)).slice(-2);
    const day = ("0" + date.getDate()).slice(-2);
    const weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"][date.getDay()];
    const hours = ("0" + date.getHours()).slice(-2);
    const minutes = ("0" + date.getMinutes()).slice(-2);
    const formatDate = `${year}年${month}月${day}日 ${weekDay} ${hours}时${minutes}分`
    return formatDate;
  }else{
    console.error('传入的startTime格式不正确，期望为合法数字类型的时间戳');
    console.log(timeStr)
    return ''
  }

}

// 只有日期
export function formatOnlyDate(timeStr) {

  if (timeStr !== null && timeStr !== undefined && timeStr !== '') {
    const timeStrss = timeStr.replace('+0000', '');
    const date = new Date(timeStrss);
    const year = date.getFullYear();
    const month = ("0" + (date.getMonth() + 1)).slice(-2);
    const day = ("0" + date.getDate()).slice(-2);
    const weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"][date.getDay()];
    const hours = ("0" + date.getHours()).slice(-2);
    const minutes = ("0" + date.getMinutes()).slice(-2);
    const formatDate = `${year}-${month}-${day}`
    return formatDate;
  }else{
    console.error('传入的startTime格式不正确，期望为合法数字类型的时间戳');
    return ''
  }

}

// 日期+时间
export function formatDateSE(timeStr) {

  if (timeStr !== null && timeStr !== undefined && timeStr !== '') {
    // const timeStrss = timeStr.replace('+0000', '');
    const date = new Date(timeStr);
    const year = date.getFullYear();
    const month = ("0" + (date.getMonth() + 1)).slice(-2);
    const day = ("0" + date.getDate()).slice(-2);
    const weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"][date.getDay()];
    const hours = ("0" + date.getHours()).slice(-2);
    const minutes = ("0" + date.getMinutes()).slice(-2);
    const formatDate = `${year}.${month}.${day}  ${hours}:${minutes}`
    return formatDate;
  }else{
    console.error('传入的startTime格式不正确，期望为合法数字类型的时间戳');
    return ''
  }

}

export function formatTimeToDate(timeStr){    //startTime:'2024年12月23日 12:00:00',
      const datePart = timeStr.split(' ')[0];
      const timePart = timeStr.split(' ')[1];
      console.log(datePart,timePart,'oooppopop')
      const year = parseInt(datePart.split('年')[0]);
      const month = parseInt(datePart.split('年')[1].split('月')[0]) - 1; // 月份需要减1，因为Date对象中月份是从0开始计数
      const day = parseInt(datePart.split('年')[1].split('月')[1].split('日')[0]);
      const hour = parseInt(timePart.split(':')[0]);
      const minute = parseInt(timePart.split(':')[1]);
      const second = parseInt(timePart.split(':')[2]);
      return new Date(year, month, day, hour, minute, second);
}

/**
 * 计算两个时间之间的差值并返回友好的时间差描述
 * @param {string} time1 - 第一个时间字符串
 * @param {string} time2 - 第二个时间字符串
 * @returns {string} 友好的时间差描述
 */
export function calculateTimeDiff(time1, time2) {
  console.log(time1,time2,'time1,time2')
  if (!time1 || !time2) return '';
  // 格式化时间字符串并转换为时间戳
  const formatTime = (timeStr) => {
      return new Date(timeStr).getTime();
  };

  const time1Ms = formatTime(time1);
  const time2Ms = formatTime(time2);

  console.log(time1Ms,time2Ms,'time1Ms,time2Ms')
  if (time1Ms <= 0 || time2Ms <= 0) return '';

  const diffValue = time1Ms - time2Ms;
  if (diffValue < 0) return '';

  const minute = 1000 * 60;
  const hour = minute * 60;
  const day = hour * 24;
  const week = 7 * day;
  const month = day * 30;

  const diffMonths = diffValue / month;
  const weekC = diffValue / (7 * day);
  const diffDays = diffValue / day;
  const hourC = diffValue / hour;
  const minC = diffValue / minute;
  console.log(diffMonths,weekC,diffDays,hourC,minC,'diffValue',diffValue)

  if (diffMonths >= 1) {
      return diffMonths <= 12 ? `${parseInt(diffDays)}天后`  : time1;
  } else if (weekC >= 1) {
      return `${parseInt(diffDays)}天后`;
  } else if (diffDays >= 1) {
      return `${parseInt(diffDays)}天后`;
  } else if (hourC >= 1) {
      return '1天内';
  } else if (minC >= 1) {
      return `${parseInt(minC)}分钟内`;
  } else {
      return '1分钟内';
  }
}

/**
 * 计算两个时间之间的差值并返回追评时间描述
 * @param {string} time1 - 第一个时间字符串(追评时间)
 * @param {string} time2 - 第二个时间字符串(原评论时间)
 * @returns {string} 追评时间描述
 */
// export function calculateTimeDiff(time1, time2) {
//   if (!time1 || !time2) return '';
  
//   try {
//     // 格式化时间字符串并转换为时间戳
//     const formatTime = (timeStr) => {
//       return new Date(timeStr.replace(/-/g, '/').replace('+0000', '')).getTime();
//     };
    
//     const time1Ms = formatTime(time1); // 追评时间
//     const time2Ms = formatTime(time2); // 原评论时间
    
//     if (time1Ms <= 0 || time2Ms <= 0) return '';
    
//     const diffMs = time1Ms - time2Ms; // 追评时间 - 原评论时间
//     if (diffMs <= 0) return '';
    
//     const minute = 60 * 1000;
//     const hour = 60 * minute;
//     const day = 24 * hour;
    
//     const diffMinutes = Math.floor(diffMs / minute);
//     const diffHours = Math.floor(diffMs / hour);
//     const diffDays = Math.floor(diffMs / day);
    
//     if (diffMinutes < 1) {
//       return '1分钟内';
//     } else if (diffMinutes < 60) {
//       return `${diffMinutes}分钟内`;
//     } else if (diffHours < 24) {
//       return `${diffHours}小时内`;
//     } else {
//       return `${diffDays}天内`;
//     }
//   } catch (error) {
//     console.error('时间计算错误:', error);
//     return '';
//   }
// }